Improved overflow support: Internal event handlers can dispatch new events #21
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What
This PR generalizes the signature of internal event handlers from
Consumer<WatchEvent>toBiConsumer<EventHandlingWatch, WatchEvent>. Thus, when internal event handlers are called, they also get access to the watch that dispatched the event.This change affects only the internals of the library (currently only the
JDK...Watchclasses, but later also the macOS code). The end-user API still expectsConsumer<WatchEvent>(orWatchEventListener).Why
Imagine an overflow event happens, so the directory needs to be rescanned. Before this PR, the event handler of the watch could look something like this (simplified code, just to make the point):
The point is that, to auto-handle overflow events, the auto-handler must access a watch to dispatch rescanning events (i.e., call the watch's
handleEventmethod). There are several alternatives to achieve this, including:JDKRecursiveDirectoryWatchThe problem with alternative 1 is that each auto-handler class should be usable with multiple watch classes (otherwise, we're potentially duplicating a lot of code). The problem with alternative 2 is that each auto-handler instance should be usable with multiple watch instances (otherwise, we're potentially duplicating a lot of handler objects that do essentially the same thing).
Thus, this PR implements alternative 3. It enables:
EventHandlingWatchinterface, which has methodhandleEvent)(Usages of these features are demonstrated in the next PR.)